//
//  MNDFinanceDescribleViewController.m
//  minidai
//
//  Created by 谭谭伟 on 17/5/21.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDFinanceDescribleViewController.h"
#import "TWPhotoViewController.h"
#import "TWPresent.h"
#import "ImageBrowserViewController.h"
#import "MNDWebViewController.h"
#import "MNDLoginUserModel.h"

@interface MNDFinanceDescribleViewController ()<UIScrollViewDelegate,UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *topTipLabel;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
/* 车辆数据图片数组 */
@property (strong, nonatomic) NSMutableArray *carPhotoAyyayM;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *checkPictureBottomMargin;

@end

@implementation MNDFinanceDescribleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.webView.scrollView.bounces = NO;
//    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.delegate = self;
    self.webView.delegate = self;
    self.webView.dataDetectorTypes = UIDataDetectorTypeNone;
    [self setupUI];
    
    if (@available(iOS 11.0, *)) {
        self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        
    }
    
//    [self setCookies];
}

#pragma mark -懒加载
- (NSMutableArray *)carPhotoAyyayM
{
    if (_carPhotoAyyayM == nil) {
        _carPhotoAyyayM = [NSMutableArray array];
    }
    return _carPhotoAyyayM;
}

- (void)setupUI {
 
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (kDevice_iPad) {
        self.checkPictureBottomMargin.constant = 120;
    }else {
        self.checkPictureBottomMargin.constant = 60;
    }
    
}

- (void)setBrrowContent:(NSString *)brrowContent {
    _brrowContent = brrowContent;
    
    NSString *rebuildStr = [self rebuildContents:brrowContent];
    
    NSLog(@"html: %@", rebuildStr);
 
    [self.webView loadHTMLString:rebuildStr baseURL:nil];

    
}

- (NSString *)rebuildContents:(NSString *)content {
    
    NSArray *arr = [content componentsSeparatedByString:@"<font>"];
    
    
    for (NSInteger i = 0; i < arr.count; i++) {
        NSString *subTitle = [NSString stringWithFormat:@"%zd.【", i];
        content = [content stringByReplacingOccurrencesOfString:subTitle withString:@""];
        content = [content stringByReplacingOccurrencesOfString:@"】" withString:@""];
        
    }
    NSURL *resourceUrl = [[NSBundle mainBundle] URLForResource:@"tender" withExtension:@"css"];
    
    if (self.isCreditor) {
        resourceUrl = [[NSBundle mainBundle] URLForResource:@"creditor" withExtension:@"css"];
    }
    NSString *css = [NSString stringWithFormat:@"<link href=\"%@\" rel=\"stylesheet\">", resourceUrl.absoluteString];
    NSString *htmlStr = [NSString stringWithFormat:@"<html><head>%@</head><body>%@</body></html>", css, content];
    return htmlStr;
}

- (void)setBrrowImg:(NSArray *)brrowImg {
    _brrowImg = brrowImg;
//    NSArray *dictImgs = brrowImg.allValues;
    for (NSString *img in brrowImg) {
        NSString *imgURL = [NSString stringWithFormat:@"%@%@",PictureBaseURL,img];
        [self.carPhotoAyyayM addObject:imgURL];
    }

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (kDevice_iPad) {
        return;
    }
    CGFloat distance = scrollView.contentOffset.y;
    if (distance < -10) {
        NSLog(@"------%f------",distance);
        self.topTipLabel.alpha = (-distance-10)/30;
        NSLog(@"------透明度%f------",self.topTipLabel.alpha);
    }
    if (distance == 0) {
        self.topTipLabel.alpha = 0;
    }
    if (distance < -40) {
        self.topTipLabel.text = @"释放回到出借详情";
    }else {
    self.topTipLabel.text = @"下拉回到出借详情";
    }
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//
//    if (self.delegate && [self.delegate respondsToSelector:@selector(describleBottomViewDidEndDragging:)]) {
//        
//        [self.delegate describleBottomViewDidEndDragging:self.webView.scrollView];
//    }
//}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if (self.delegate && [self.delegate respondsToSelector:@selector(describleBottomViewDidEndDragging:)]) {
        
        [self.delegate describleBottomViewDidEndDragging:self.webView.scrollView];
    }
}

- (IBAction)checkBrrowImg:(UIButton *)sender {
//    __weak typeof(self) weakSelf=self;
//    [ImageBrowserViewController show:self type:PhotoBroswerVCTypeModal index:0 imagesBlock:^NSArray *{
//        return weakSelf.carPhotoAyyayM;
//    }];
    if (self.carPhotoAyyayM > 0) {
        TWPhotoViewController *photoVC = [[TWPhotoViewController alloc]init];
        photoVC.photoArray = self.carPhotoAyyayM;
        TWPresent *present = [TWPresent sharedTWPresent];
        photoVC.modalPresentationStyle = UIModalPresentationCustom;
        photoVC.transitioningDelegate = present;
        [self presentViewController:photoVC animated:YES completion:nil];
    }else {
        [NetworkProgressHUD showHudMsg:@"暂无图片"];
    }
    
    
}


#pragma mark - UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    //    取消长按webView上的链接弹出actionSheet的问题
    [webView stringByEvaluatingJavaScriptFromString: @"document.documentElement.style.webkitUserSelect='none';"];
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(bottomViewDidEndDragging:)]) {
//        
//        [self.delegate bottomViewDidEndDragging:self.tableView];
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setCookies {
    if ([MNDLoginUserModel isUserLogin]) {
        //        [self clearWebViewCookies];
        
        MNDLoginUserModel *user = [MNDLoginUserModel currentLoginUser];
        
        NSDictionary *dictCookieUId;
        
//        if ([userDict objectForKey:@"encode_user_code"]) {
//            dictCookieUId = @{NSHTTPCookieName: @"mobile_user_code",
//                              NSHTTPCookieValue: userDict[@"encode_user_code"],
//                              NSHTTPCookiePath: @"/",
//                              NSHTTPCookieVersion: @"0",
//                              NSHTTPCookieDomain: @".miniwangdai.com"
//                              };
//        } else {
            dictCookieUId = @{NSHTTPCookieName: @"user_code",
                              NSHTTPCookieValue: user.user_code,
                              NSHTTPCookiePath: @"/",
                              NSHTTPCookieVersion: @"0",
                              NSHTTPCookieDomain: @".miniwangdai.com"
                              };
//        }
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:dictCookieUId];
        
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
